import cv2
import numpy as np

# 读取图像
image = cv2.imread('images/22.jpg')

# 将图像从BGR转换到HSV颜色空间
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# 设定要筛选的颜色的HSV阈值范围
# 例如筛选绿色
lower_red = np.array([0, 127, 128])  # 红色阈值下界
higher_red = np.array([10, 255, 255])  # 红色阈值上界

# 根据阈值构建掩模
mask = cv2.inRange(hsv, lower_red, higher_red)

# 对原图像和掩模进行位运算，得到只包含绿色的图像
green_only = cv2.bitwise_and(image, image, mask=mask)

gray = cv2.cvtColor(green_only,cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray,25,0.01,10)
corners = np.int0(corners)
for i in corners:
    x,y = i.ravel()
    cv2.circle(green_only,(x,y),3,255,-1)

cv2.imshow('Green Only', green_only)
cv2.waitKey(0)
cv2.destroyAllWindows()